//
//  WaterFallFlowViewController.m
//  MyProject
//
//  Created by yun on 2018/7/11.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "WaterFallFlowViewController.h"
#import "LYCollectionViewLayout.h"
#import "WaterFallFlowCollectionViewCell.h"

@interface WaterFallFlowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,LYCollectionViewLayoutDelegate>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *dataArray;


@end

@implementation WaterFallFlowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [NSMutableArray new];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    for (NSDictionary *dict in array) {
        WaterFallFlowModel *model = [WaterFallFlowModel mj_objectWithKeyValues:dict];
        [self.dataArray addObject:model];
    }
    
    LYCollectionViewLayout *layout = [[LYCollectionViewLayout alloc] init];
    layout.delegate = self;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"WaterFallFlowCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CellWithReuseIdentifier];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}

static NSString * const CellWithReuseIdentifier = @"CellWithReuseIdentifier";
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WaterFallFlowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellWithReuseIdentifier forIndexPath:indexPath];
    [cell setupModel:self.dataArray[indexPath.item]];
    return cell;
}

- (CGFloat)ly_collectionView:(UICollectionView *)collectionView layout:(LYCollectionViewLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth{
    WaterFallFlowModel *model = self.dataArray[indexPath.item];
    return model.h;
}

- (NSInteger)ly_collectionView:(UICollectionView *)collectionView layout:(LYCollectionViewLayout *)collectionViewLayout columnForSectionAtIndex:(NSInteger)section{
    
    return 2;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
